/****************************************************
 * Description: ServiceImpl for 菜单
 * Copyright:   Copyright (c) 2013
 * Company:     beiwaionline
 * @author      bfsu
 * @version     1.0
 * @see
	HISTORY
    *  2013-08-08 bfsu Create File
**************************************************/

package com.bfsuolframework.security.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bfsuolframework.security.dao.MenuDAO;
import com.bfsuolframework.security.entity.Menu;
import com.bfsuolframework.security.service.MenuService;
import com.bfsuolframework.core.controller.support.DefaultQueryParam;
import com.bfsuolframework.core.controller.support.QueryParam;
import com.bfsuolframework.core.service.ServiceSupport;


@Service
public class MenuServiceImpl extends ServiceSupport<Menu> implements MenuService {

    @Autowired
    private MenuDAO menuDAO;
    
    @Override
    public MenuDAO getDao() {
        return menuDAO;
    }

    /*
     * (non-Javadoc)
     * @see MenuService#deleteMenu(Menu)
     */
	public boolean deleteMenu(Long menuId) {
		if(null == menuId)
			return false;
		QueryParam query = new DefaultQueryParam();
		List<Menu> menus = menuDAO.findAll(query.addQuery("parentId!eq@l", menuId));//查找子菜单的id为此菜单的id:memuid	
		if(menus.size() <= 0){//如果没有子菜单直接删除此菜单
			menuDAO.delete(menuId);
			return true;
		}else{
			for(Menu menu : menus){//如果当前菜单有子菜单，遍历他的子菜单
				this.deleteMenu(menu.getId());//递归调用，寻找子菜单里是否还有子菜单,并删除其中的子菜单
			}
			menuDAO.delete(menuId);//删除顶端的菜单
			return true;
		}
		
	}


}